package com.gao.activitidemo.activititest;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.*;

/**
 * @author gaofeng
 * @date 2022年09月18日 9:26
 */
public class CandidateTaskTest {

    /**
     * 流程部署
     */
    @Test
    public void testActivitiDeployment() {
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2. 获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        // 3.进行流程部署, 定义一个流程名称, 把bpmn和png部署到数据库中
        Deployment deploy = repositoryService.createDeployment().name("出差申请流程")
                .addClasspathResource("bpmn/evection-candidate.bpmn20.xml")
                .addClasspathResource("bpmn/evection-candidate.png")
                .deploy();
        // 4.输出部署信息
        System.out.println("流程部署id:" + deploy.getId());
        System.out.println("流程部署id:" + deploy.getName());
    }

    /**
     * 启动流程实例 设置流程变量
     */
    @Test
    public void testStartProcess() {
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 3.根据流程定义的id启动
        ProcessInstance evection = runtimeService.startProcessInstanceByKey("evection-candidate");
        // 4.输出
        System.out.println("流程定义的id: " + evection.getProcessDefinitionId());
        System.out.println("当前活动的id: " + evection.getActivityId());
    }

    /**
     * 完成个人任务--张三
     */
    @Test
    public void testCompletePersonalTask() {
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2.创建TaskService
        TaskService taskService = processEngine.getTaskService();
        // 3.根据任务id完成任务
        // 根据流程key和负责人查任务,根据taskId完成任务
        Task task = taskService.createTaskQuery().processDefinitionKey("evection-candidate").taskCandidateOrAssigned("张三").singleResult();
        if (Objects.nonNull(task)) {
            taskService.complete(task.getId());
            // 第二种,完成任务的时候设置流程变量
//            taskService.complete(task.getId(), new HashMap<>());
        }
    }

    /**
     * 查询组任务
     */
    @Test
    public void findGroupTasks() {
        // 流程定义key
        String key = "evection-candidate" ;
        // 任务候选人
        String candidateUser = "王五" ;
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2. 获取RepositoryService
        TaskService taskService = processEngine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey(key).taskCandidateUser(candidateUser).list();
        for (Task task : tasks) {
            System.out.println("流程实例id" + task.getProcessInstanceId());
            System.out.println("任务id" + task.getId());
            System.out.println("任务负责人" + task.getAssignee());
        }
    }

    /**
     * 候选人拾取任务
     *  该任务会变成候选人的个人任务
     */
    @Test
    public void ClaimTask() {
        // 流程定义key
        String taskId = "700002" ;
        // 任务候选人
        String candidateUser = "王五" ;
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2. 获取RepositoryService
        TaskService taskService = processEngine.getTaskService();
        Task tasks = taskService.createTaskQuery().taskId(taskId).taskCandidateUser(candidateUser).singleResult() ;
        if (Objects.nonNull(tasks)) {
            taskService.claim(taskId, candidateUser);
            System.out.println("任务" + taskId + "用户" + candidateUser + "拾取完成");
        }
    }

    /**
     * 归还任务
     *  把负责人设置为null
     */
    @Test
    public void assigneeToGroupTask() {
        // 流程定义key
        String taskId = "700002" ;
        // 任务候选人
        String candidateUser = "王五" ;
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2. 获取RepositoryService
        TaskService taskService = processEngine.getTaskService();
        Task tasks = taskService.createTaskQuery().taskId(taskId).taskCandidateUser(candidateUser).singleResult() ;
//        if (Objects.nonNull(tasks)) {
            taskService.setAssignee(taskId, null);
            System.out.println("任务" + taskId + "用户" + candidateUser + "归还完成");
//        }
    }

    /**
     * 任务的交接
     *  把负责人设置为候选人
     */
    @Test
    public void assigneeToCandidateUser() {
        // 流程定义key
        String taskId = "700002" ;
        // 任务责任人
        String assignee = "王五" ;
        // 任务候选人
        String candidateUser = "李四" ;
        // 1.创建ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 2. 获取RepositoryService
        TaskService taskService = processEngine.getTaskService();
        Task tasks = taskService.createTaskQuery().taskId(taskId).taskCandidateUser(assignee).singleResult() ;
        if (Objects.nonNull(tasks)) {
        taskService.setAssignee(taskId, candidateUser);
        System.out.println("任务" + taskId + "用户" + candidateUser + "归还完成");
        }
    }


}
